package com.example.user.myhjjf_app.view;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.PopupMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.user.myhjjf_app.HJJF_Url;
import com.example.user.myhjjf_app.R;
import com.example.user.myhjjf_app.model.Model_Regional;
import com.example.user.myhjjf_app.model.Model_zhcsg;
import com.example.user.myhjjf_app.presenter.Implementation_operation;
import com.example.user.myhjjf_app.presenter.SiteOperation;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class Activity_zhDkAdd extends AppCompatActivity {

    @BindView(R.id.btn_back)
    LinearLayout btnBack;
    @BindView(R.id.tx_title)
    TextView txTitle;
    @BindView(R.id.btn_edit)
    LinearLayout btnEdit;
    @BindView(R.id.et_dk_name)
    EditText etDkName;
    @BindView(R.id.et_dk_code)
    EditText etDkCode;
    @BindView(R.id.et_dk_ms)
    EditText etDkMs;
    @BindView(R.id.bt_add_dk)
    Button btAddDk;
    SiteOperation siteOperation;
    Model_zhcsg.ValueBean valueBean;
    String type,tiaojian;
    String url;
    int position;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zh_dk_add);
        ButterKnife.bind(this);
        Bundle bundle = getIntent().getExtras();
        siteOperation = new Implementation_operation(this);
        type = bundle.getString("type");
        tiaojian = bundle.getString("tj");
        position = bundle.getInt("position");
        if (!type.equals("add")){
            valueBean = (Model_zhcsg.ValueBean) bundle.getSerializable("va");
            etDkName.setText(valueBean.getName());
            etDkCode.setText(valueBean.getPort());
            etDkMs.setText(valueBean.getDescription());
            if (type.equals("select")) {
                txTitle.setText("端口详情");
                etDkName.setEnabled(false);
                etDkCode.setEnabled(false);
                etDkMs.setEnabled(false);
                btAddDk.setVisibility(View.GONE);
            }
        }else {
            txTitle.setText("添加端口");
        }
    }

    @OnClick({R.id.btn_back, R.id.btn_edit, R.id.bt_add_dk})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_edit:
                showMenu(btnEdit);
                break;
            case R.id.bt_add_dk:
                siteOperation = new Implementation_operation(Activity_zhDkAdd.this);
                HashMap map = new HashMap();
                url = HJJF_Url.getHJJF_Url().login("api/data/odp_add.do");
                if (tiaojian.equals("gw2")){
                    url= HJJF_Url.getHJJF_Url().login("api/data/ddf_add.do");
                }
                if (type.equals("select")) {
                    url = HJJF_Url.getHJJF_Url().login("api/data/odp_edit.do");
                    if (tiaojian.equals("gw2")){
                        url= HJJF_Url.getHJJF_Url().login("api/data/ddf_edit.do");
                    }
                    map.put("id", valueBean.getId());
                }
                map.put("siteId", Model_Regional.getRegional().getSiteId());
                map.put("name", etDkName.getText().toString());
                map.put("port", etDkCode.getText().toString());
                map.put("description", etDkMs.getText().toString());
                map.put("position", "" + position);
                Iterator iterator = map.entrySet().iterator();
                int i = 0;
                while (iterator.hasNext()){
                    Map.Entry entry = (Map.Entry) iterator.next();
                    if (entry.getValue().equals("")){
                        Toast.makeText(this, "所有选项不能为空~~", Toast.LENGTH_SHORT).show();
                        i=0;
                        break;
                    }else {
                        i=1;
                    }
                }
                if (i==1){
                    siteOperation.Add(map,url);
                }
                break;
        }
    }



    private void showMenu(View view){
        // View当前PopupMenu显示的相对View的位置
        PopupMenu popupMenu = new PopupMenu(this,view);
        //menu布局
        popupMenu.getMenuInflater().inflate(R.menu.menu,popupMenu.getMenu());
        //点击事件
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()){
                    case R.id.btn_menu_edit:
                        txTitle.setText("端口编辑");
                        etDkName.setEnabled(true);
                        etDkCode.setEnabled(true);
                        etDkMs.setEnabled(true);
                        btAddDk.setVisibility(View.VISIBLE);
                        break;
                    case R.id.btn_menu_delete:
                        String url = HJJF_Url.getHJJF_Url().login("api/data/odp_drop.do");
                        if (tiaojian.equals("gw2")){
                            url= HJJF_Url.getHJJF_Url().login("api/data/ddf_drop.do");
                        }
                        siteOperation.delete(url,valueBean.getId());
                        break;
                }
                return false;
            }
        });
        //关闭事件
        popupMenu.setOnDismissListener(new PopupMenu.OnDismissListener() {
            @Override
            public void onDismiss(PopupMenu menu) {

            }
        });
        popupMenu.show();
    }
}
